const fs = require("fs");


let datas = JSON.parse(fs.readFileSync("cfg_co_monster_hp.json") + "");
fs.writeFileSync("啊啊.json", JSON.stringify(datas));

function arr(keys, data) {
	let result = new Array(data.length);
	for (let i = 0; i < data.length; i++) {
		let v = data[i];
		result[i] = objj(keys, v);
	}

	return result;
}

function obj(keys, data) {
	let result = {};
	for (let k in data) {
		let v = data[k];
		result[k] = objj(keys, v);
	}

	return result;
}

function objj(keys, data) {
	let result = [];
	for (let k in data) {
		let v = data[k];
		let index = keys.indexOf(k);
		if (index < 0)
			index = keys.push(k) - 1;

		result.push(index, v);
	}

	return result;
}

function get(data) {
	let keys = [];
	let result = null;
	if (Array.isArray(data)) {
		result = arr(keys, data);
	} else if (typeof data === "object") {
		result = obj(keys, data);
	}

	return [keys, result];
}
let temp = get(datas);
let d1 = JSON.stringify(datas).length;
let d2 = JSON.stringify(temp).length;
console.log("压缩比例", (d1 - d2) / d1 * 100);
fs.writeFileSync("啊啊2.json", JSON.stringify(temp, null, 4));


function get2(data) {
	let keys = data[0];
	let datas = data[1];
	if (Array.isArray(datas))
		return arr2(keys, datas);
	else if (typeof datas === "object")
		return obj2(keys, datas);
}

function arr2(keys, data) {
	let result = new Array(data.length);
	for (let i = 0; i < data.length; i++) {
		let v = data[i];
		result[i] = objj2(keys, v);
	}

	return result;
}

function obj2(keys, data) {
	let result = {};
	for (let k in data) {
		let v = data[k];
		result[k] = objj2(keys, v);
	}

	return result;
}

function objj2(keys, data) {
	let result = {};
	for (let i = 0; i < data.length; i += 2) {
		result[keys[data[i]]] = data[i + 1];
	}


	return result;
}

let temp2 = get2(temp);
console.log(d1 === JSON.stringify(temp2).length);